/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewCommande.java
 *
 * Created on 18 mai 2011, 14:18:15
 */

package View;

import Controller.ControllerCommande;
import Model.Utilisateur;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sun.util.BuddhistCalendar;

/**
 *
 * @author aj48640
 */
public class ViewCommande extends javax.swing.JFrame 
{
    
    private ControllerCommande controllerCommand;
    private DefaultTableModel model;
    private boolean placerCommande = false;
    private Utilisateur client;
    
    
    /** Creates new form ViewCommande */
    public ViewCommande(String adresseClient, String nomClient) 
    {
        initComponents();
        
        
        Init( nomClient,adresseClient);
        
    }
    
    private void Init ( String nomClient, String adresseClient )
    {
        
        
        
        //on instancie noter model
        model = new DefaultTableModel();
        model.addColumn("Plat");
        model.addColumn("Quantite");
        model.addColumn("Restaurant");
        
        dgvCommande.setModel(model);
        
        client = new Utilisateur(nomClient, adresseClient, false);
        
        controllerCommand = ControllerCommande.getInstance();
        
        controllerCommand.DebuterCommande(client);
        
        
             
        //on met l'adresse du client
        this.txtAdresse.setText(adresseClient);
        
        //nous allons cherchers nos restaurant
        ArrayList<String> restaurants = controllerCommand.GetRestaurants();
        
        
        for ( int i = 0; i < restaurants.size();i++)
            this.cboRestaurants.addItem(restaurants.get(i));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cboRestaurants = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        cboPlats = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cboQuantite = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        btnAjouter = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        dgvCommande = new javax.swing.JTable();
        chkAdresse = new javax.swing.JCheckBox();
        txtAdresse = new javax.swing.JTextField();
        btnPlacerCommande = new javax.swing.JButton();
        cboYear = new javax.swing.JComboBox();
        cboMonth = new javax.swing.JComboBox();
        cboDay = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cboHour = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cboMinute = new javax.swing.JComboBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuItemFermer = new javax.swing.JMenuItem();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        cboRestaurants.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboRestaurantsActionPerformed(evt);
            }
        });

        jLabel1.setText("Restaurant");

        jLabel2.setText("Plats");

        cboQuantite.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        cboQuantite.setValue(1);

        jLabel3.setText("Quantité : ");

        btnAjouter.setText("Ajouter");
        btnAjouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAjouterActionPerformed(evt);
            }
        });

        dgvCommande.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Plat", "Quantité"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        dgvCommande.setEditingRow(0);
        dgvCommande.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                dgvCommandeKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(dgvCommande);
        dgvCommande.getColumnModel().getColumn(0).setResizable(false);
        dgvCommande.getColumnModel().getColumn(1).setResizable(false);

        chkAdresse.setSelected(true);
        chkAdresse.setText("Adresse par défaut");
        chkAdresse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkAdresseActionPerformed(evt);
            }
        });

        txtAdresse.setEnabled(false);

        btnPlacerCommande.setText("Placer la commande");
        btnPlacerCommande.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnPlacerCommandeMouseClicked(evt);
            }
        });
        btnPlacerCommande.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlacerCommandeActionPerformed(evt);
            }
        });

        cboYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2011", "2012", "2013", "2014", "2015" }));

        cboMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        cboDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        jLabel4.setText("Date de livraison : ");

        cboHour.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24" }));

        jLabel5.setText(":");

        cboMinute.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "15", "30", "45" }));

        jMenu1.setText("Fichier");

        menuItemFermer.setText("Fermer");
        menuItemFermer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemFermerActionPerformed(evt);
            }
        });
        jMenu1.add(menuItemFermer);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 694, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, txtAdresse, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 694, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(cboPlats, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 361, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel3)
                                .add(2, 2, 2)
                                .add(cboQuantite, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnAjouter))
                            .add(cboRestaurants, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 361, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, chkAdresse)
                    .add(layout.createSequentialGroup()
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cboDay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cboMonth, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cboYear, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cboHour, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cboMinute, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(btnPlacerCommande)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(cboRestaurants, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(cboPlats, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel3)))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(cboQuantite, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(btnAjouter)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 185, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(chkAdresse)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(txtAdresse, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 86, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnPlacerCommande)
                        .addContainerGap(34, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel4)
                            .add(cboDay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(cboMonth, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(cboYear, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(cboHour, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel5)
                            .add(cboMinute, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAjouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAjouterActionPerformed
 
        model.addRow(
               new Object[]
                       {
                           cboPlats.getSelectedItem(),
                           cboQuantite.getValue(),
                           cboRestaurants.getSelectedItem()
                          }
               );
        
       
    }//GEN-LAST:event_btnAjouterActionPerformed

    private void cboRestaurantsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboRestaurantsActionPerformed
        
        FillMenu(controllerCommand.GetMenu(this.cboRestaurants.getSelectedIndex()));
    }//GEN-LAST:event_cboRestaurantsActionPerformed

    private void dgvCommandeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dgvCommandeKeyPressed
        switch(evt.getKeyCode())
        {
            //Delete Key
            case 127:
                if (dgvCommande.getSelectedRow() != -1)
                {
                
                    model.removeRow(dgvCommande.getSelectedRow());
                 }
                break;
        }
    }//GEN-LAST:event_dgvCommandeKeyPressed

    private void chkAdresseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkAdresseActionPerformed
         this.txtAdresse.setEnabled(!this.chkAdresse.isSelected());
    }//GEN-LAST:event_chkAdresseActionPerformed

    private void btnPlacerCommandeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlacerCommandeActionPerformed
    }//GEN-LAST:event_btnPlacerCommandeActionPerformed

    private void btnPlacerCommandeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPlacerCommandeMouseClicked
        
        //Nous prenons chaque plat et quantité du tableau et nous l'ajoutons 
        //dans notre objet commande.
        Date d;
        if ( !placerCommande)
        {
            if ((d =ValiderDate() )!= null)
            {
                if ( !chkAdresse.isSelected())
                {
                   controllerCommand.ChangerAdresse(this.txtAdresse.getText());
                }
                
                


                //on ajoute nos plats dans la commande
                for ( int i = 0; i < model.getRowCount(); i++ )
                {
                    controllerCommand.AjouterPlat(Integer.parseInt(model.getValueAt(i, 1).toString()), model.getValueAt(i, 0).toString(), model.getValueAt(i, 2).toString());
                }

                //on affiche un "résumé"
                JOptionPane.showMessageDialog(null, controllerCommand.FinaliserCommande(d));

                placerCommande = true;
                this.setVisible(false);
            }
        }
        
                
    }//GEN-LAST:event_btnPlacerCommandeMouseClicked

    private Date ValiderDate()
    {
        try
        {
            String day = cboDay.getSelectedItem().toString();
            String month = cboMonth.getSelectedItem().toString();
            String year = cboYear.getSelectedItem().toString();
            String time = cboHour.getSelectedItem().toString() + ":" + cboMinute.getSelectedItem().toString();

            Date d = new Date(year+"/"+month+"/"+day+" " + time);
            
            if (d.after(new Date()))
            {
               return d; 
            }
            else
            {
                JOptionPane.showMessageDialog(null,"La date entrée est inférieur à la date actuelle !");
                return null;
            }
            
        }
        catch (Exception e)
        {
            return null;
        }
        
                
    }
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
      
        
    }//GEN-LAST:event_formWindowClosing

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
       
    }//GEN-LAST:event_formWindowClosed

    private void menuItemFermerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemFermerActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_menuItemFermerActionPerformed

    /**
     * Permet de remplir les combox box avec les plats
     * @param plats 
     */
    private void FillMenu(ArrayList<String> plats)
    {
        cboPlats.removeAllItems();
        for ( int i = 0; i < plats.size();i++)
            this.cboPlats.addItem(plats.get(i));
    }

    
    public static void main(final String args[]) 
    {
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new ViewCommande(args[0], args[1]).setVisible(true);
            }
            
      
        });
    }
    
    private boolean Authentifier() 
    {
        String nom = null;
        
        do
        {
            nom = JOptionPane.showInputDialog("Entrez votre nom :");
       } while ( nom != null && !controllerCommand.AuthentifierClient(nom));
        
       return nom != null;
           
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAjouter;
    private javax.swing.JButton btnPlacerCommande;
    private javax.swing.JComboBox cboDay;
    private javax.swing.JComboBox cboHour;
    private javax.swing.JComboBox cboMinute;
    private javax.swing.JComboBox cboMonth;
    private javax.swing.JComboBox cboPlats;
    private javax.swing.JSpinner cboQuantite;
    private javax.swing.JComboBox cboRestaurants;
    private javax.swing.JComboBox cboYear;
    private javax.swing.JCheckBox chkAdresse;
    private javax.swing.JTable dgvCommande;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem menuItemFermer;
    private javax.swing.JTextField txtAdresse;
    // End of variables declaration//GEN-END:variables



}
